#include <allegro.h>
#include <stdio.h>
#include "gang.h"


BITMAP* load_background_bmp(BITMAP *Background)
{
	Background=load_bitmap("/home/ines/jeu/images/etage1.bmp", NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	
	return (Background);
}


void draw_background(BITMAP *Background,BITMAP * Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}

void load_gangsters(Gangsters *gang)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "gang%d.bmp", i);
		gang->tab_gang_right[i] = load_bitmap(buf, NULL);
		//sprintf(buf, "left%d.bmp", i);
		//gang->tab_gang_left[i] = load_bitmap(buf, NULL);
	}                         
}

void destroy_gangsters(Gangsters *gang)
{
       int i;
	for (i = 0; i < NB_IMAGES; i++) 
        {
	 destroy_bitmap(gang->tab_gang_right[i]);
         //destroy_bitmap(gang->tab_gang_left[i]);
	}
}


void init_gangsters(Gangsters *gang)
{
	gang->x = 0+(gang->tab_gang_right[0]->w);
        gang->y = 350;
	gang->direction = 0;
}

void move_gangsters_right(Gangsters *gang,int *compteur,BITMAP * Background)
{
	if ((gang->x) < (SCREEN_W - gang->tab_gang_right[0]->w) && ((gang->direction)==0))
        {
                *compteur+=1;
                (gang->x)+=1;
                usleep(10000);
                if((gang->x) == (SCREEN_W - gang->tab_gang_right[0]->w))
                {
			gang->direction = 1; //changer vers gauche   
			(gang->x)=0;
		}	
	}
}

/*void move_gangsters_left(Gangsters *gang,int *compteur,BITMAP * Background)
{
	if ((gang->x > 0) && (gang->direction==1))
        {
                *compteur+=1;
                
                (gang->x)-=1;
                usleep(5000);
                if  (gang->x = 0 + gang->tab_gang_left[1]->w)
                gang->direction = 0; // changer vers droite
	}
}*/


void move_gangsters(Gangsters *gang,int *compteur,BITMAP * Background)
{
	move_gangsters_right(gang,compteur,Background);
	//move_gangsters_left(gang,compteur,Background);
}


void draw_gangsters(Gangsters gang,BITMAP *Buffer, int compteur)
{
	if (gang.direction == 0)
	{ 
		//moving right
		draw_sprite(Buffer, gang.tab_gang_right[compteur % (NB_IMAGES-1) ],gang.x, gang.y);
	}

	/*if(gang.direction == 1)
	{ 
		//moving left
		draw_sprite(Buffer, gang.tab_gang_left[compteur % (NB_IMAGES-1) ],gang.x, gang.y);
	}*/
}


